<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Goods;
use App\Models\Order;
use App\Traits\ApiResponse;
use Illuminate\Http\Request;
use App\Models\Match;
use DB;

class GoodsController extends Controller
{
    use ApiResponse;

    public function index(Request $request)
    {
        $goods = Goods::query()
            ->leftJoin('tb_category', 'tb_category.id', '=', 'tb_goods.type_id')
            ->select('tb_goods.*', 'tb_category.name as category_name')
            ->orderBy('tb_goods.goods_id', 'DESC')
            ->paginate();
        return view('admin.goods.index', [
            'goods' => $goods
        ]);
    }

    public function getList(Request $request)
    {
        $orders = Goods::query()
            ->leftJoin('tb_category', 'tb_category.id', '=', 'tb_goods.type_id')
            ->select('tb_goods.*', 'tb_category.name as category_name')
            ->orderBy('tb_goods.goods_id', 'DESC')
            ->paginate();
        return $this->success($orders);
    }

    public function create(Request $request)
    {
        $categories = Category::query()->get();
        $goods = Goods::query()->with('matches')->where('goods_id', $request['goods_id'])->first();
        return view('admin.goods.create', [
            'categories' => $categories,
            'goodsId' => $request['goods_id'],
            'goods' => $goods
        ]);
    }

    public function detail(Request $request)
    {
        $goods = Goods::query()->with('matches')->where('goods_id', $request['goods_id'])->first();
        return $this->success($goods);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|string|max:255',
            'price' => 'required|numeric|max:999999999',
            'pic' => 'required|string|max:255',
            'type_id' => 'required|integer',
            'details' => 'required|string|max:255',
        ], [

        ]);
        DB::beginTransaction();
        try {
            if ($request['goods_id']) {
                $goods = Goods::query()->where('goods_id', $request['goods_id'])->first();
                if (!$goods) {
                    throw  new \Exception('记录不存在');
                }
                $goods->update([
                    'name' => $request['name'],
                    'price' => $request['price'],
                    'pic' => $request['pic'] ?? '默认',
                    'type_id' => $request['type_id'],
                    'details' => $request['details'],
                ]);
                Match::query()->where('goods_id', $goods->goods_id)->delete();
            } else {
                $goods = Goods::query()->create([
                    'name' => $request['name'],
                    'price' => $request['price'],
                    'pic' => $request['pic'] ?? '',
                    'type_id' => $request['type_id'],
                    'details' => $request['details'],
                ]);
            }
//            print_r($request['attrs']);
//            die;
            foreach ($request['attrs'] as $item) {
                Match::query()->create([
                    'goods_id' => $goods->goods_id,
                    'color' => $item['color'],
                    'size' => $item['size'],
                    'num' => $item['num'],
                ]);
            }
            DB::commit();
        } catch (\Throwable $throwable) {
            DB::rollBack();
            \Log::error($throwable);
            return $this->error($throwable->getMessage());

        }
        return $this->success();
    }

    public function destroy(Request $request)
    {
        $goodsId = $request['goods_id'] ?? 0;
        Goods::query()->where('goods_id', $goodsId)->delete();
        Match::query()->where('goods_id', $goodsId)->delete();
        return $this->success([], '删除成功');
    }
}
